#include "commerrno.h"
#include "CWorldBossCfg.h"

void CWorldBossCfg::InitWorldBossCfg()
{
    _astAttackCfg.Clear();//减少遍历宽度

    _astRewardCfg.m_iUsedCount = _astRewardCfg.GetCount();
    for (int i = 0; i < _astRewardCfg.Size(); ++i)
    {
        _astRewardCfg[i].Clear();
    }
}

int CWorldBossCfg::AddAttackConfig(CWorldBossCfgAttack& stCfgData)
{
    int iRet = 0;

    if (!stCfgData.IsValidAttackID())
    {
        return ERROR_WORLD_BOSS_OVER_RESID;
    }
    
    if (_astAttackCfg.Size() >= _astAttackCfg.GetCount())
    {
        return ERROR_WORLD_BOSS_FULL_CFG;
    }

    //这个排序必须要保证
    for (int i = 0; i < _astAttackCfg.Size(); ++i)
    {
        if (stCfgData.GetAttackID() <= _astAttackCfg[i].GetAttackID())
        {
            return ERROR_WORLD_BOSS_REPEAT_RESID;
        }
    }

    iRet = _astAttackCfg.AddOneItem(stCfgData);
    if (iRet < 0)
    {
        CT_ERROR(("world boss add attack config(%d) error(%d)",
                  stCfgData.GetAttackID(), iRet));
        return RET_SYSTEM_FAIL;
    }

    return 0;
}

int CWorldBossCfg::FindAttackConfig(int iAttackID)
{
    if (iAttackID <= 0)
    {
        CT_WARNING(("Attack id(%d) invalid!", iAttackID));
        return -1;//因为返回下标 所以这里必须返回负数
    }

    //左开右闭区间的搜索
    for (int i = 0; i < _astAttackCfg.Size(); )//注意不要自增放在循环内
    {
        if (iAttackID > _astAttackCfg[i].GetAttackID())
        {
            ++i;
        }
        else if (iAttackID <= _astAttackCfg[i].GetAttackID())
        {
            return i;
        }
    }

    return _astAttackCfg.Size() - 1;
}

CWorldBossCfgAttack& CWorldBossCfg::GetAttackConfig(int iIndex)
{
    return _astAttackCfg[iIndex];
}

int CWorldBossCfg::AddRewardConfig(CWorldBossCfgReward& stCfgData)
{
    if (!stCfgData.IsValidLevel())
    {
        return ERROR_WORLD_BOSS_OVER_RESID;
    }
    
    if (_astRewardCfg[stCfgData.GetLevel() - 1].IsValidLevel())
    {
        return ERROR_WORLD_BOSS_REPEAT_RESID;
    }

    _astRewardCfg[stCfgData.GetLevel() - 1] = stCfgData;

    return 0;
}

int CWorldBossCfg::FindRewardConfig(int iLevel)
{
    if (iLevel <= 0 || iLevel > MAX_ROLE_LEVEL)
    {
        CT_WARNING(("reward config level(%d) invalid!", iLevel));
        return -1;//因为返回下标 所以这里必须返回负数
    }

    if (iLevel > _astRewardCfg.Size())
    {
        CT_WARNING(("reward config level(%d) empty!", iLevel));
        return -1;
    }

    if (!_astRewardCfg[iLevel - 1].IsValidLevel())
    {
        CT_WARNING(("reward config level(%d) empty!", iLevel));
        return -1;
    }

    return iLevel - 1;
}

CWorldBossCfgReward& CWorldBossCfg::GetRewardConfig(int iIndex)
{
    return _astRewardCfg[iIndex];
}

